package com.xc.sys.service;

import com.xc.common.object.PageInfo;
import com.xc.db.tables.SysOperationLog;
import com.xc.db.tables.records.SysOperationLogRecord;
import com.xc.sys.entity.SysOperationLogEntity;
import java.util.UUID;
import org.jooq.Condition;
import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.SelectField;
import org.jooq.SortField;
import org.jooq.TableField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SysOperationLogService {
	@Autowired
	private DSLContext dsl;
	private SysOperationLog OPERATIONLOG = SysOperationLog.SYS_OPERATION_LOG.as("OPERATIONLOG");
  
	public Record getById(Integer id){
		Record record = this.dsl.select(new SelectField[0]).from(this.OPERATIONLOG).where(new Condition[] { this.OPERATIONLOG.ID.eq(id) }).fetchOne();
		return record;
	}
  
	public SysOperationLogEntity getEntityById(Integer id) {
		Record record = this.dsl.select(new SelectField[0]).from(this.OPERATIONLOG).where(new Condition[] { this.OPERATIONLOG.ID.eq(id) }).fetchOne();
		SysOperationLogEntity org = null;
		if (record != null) {
			org = (SysOperationLogEntity)record.into(SysOperationLogEntity.class);
		}
		return org;
	}
  
	public PageInfo<SysOperationLogEntity> select(Condition cond, PageInfo<SysOperationLogEntity> pageInfo, SortField<?>... orderby) {
	  	Result<Record> result = null;
	  	if (cond == null) {
	  		result = this.dsl.select(new SelectField[0]).from(this.OPERATIONLOG).orderBy(orderby).limit(pageInfo.getLimit().intValue()).offset(pageInfo.getBeginNum()).fetch();
	  	} else {
	  		result = this.dsl.select(new SelectField[0]).from(this.OPERATIONLOG).where(new Condition[] { cond }).orderBy(orderby).limit(pageInfo.getLimit().intValue()).offset(pageInfo.getBeginNum()).fetch();
	  	}
	  	pageInfo.setResult(result);
	  	pageInfo.setList(result.into(SysOperationLogEntity.class));
	  	return pageInfo;
	}
  
	public Result<Record> select(Condition cond, TableField<SysOperationLogRecord, ?>... orderby) {
	    Result<Record> result = null;
	    if (cond == null) {
	      result = this.dsl.select(new SelectField[0]).from(this.OPERATIONLOG).orderBy(orderby).fetch();
	    } else {
	      result = this.dsl.select(new SelectField[0]).from(this.OPERATIONLOG).where(new Condition[] { cond }).orderBy(orderby).fetch();
	    }
	    return result;
	}
  
	public Long count(Condition cond) {
	    Long count = null;
	    count = (Long)this.dsl.selectCount().from(this.OPERATIONLOG).where(new Condition[] { cond }).fetchOneInto(Long.class);
	    return count;
	}
  
	public void update(SysOperationLogEntity entity){
	    SysOperationLogRecord record = new SysOperationLogRecord();
	    record.from(entity);
	    this.dsl.executeUpdate(record);
	}
  
	public void update(Record record) {
	    SysOperationLogRecord sysOperationLogRecord = new SysOperationLogRecord();
	    sysOperationLogRecord.from(record);
	    this.dsl.executeUpdate(sysOperationLogRecord);
	}
  
	public void insert(SysOperationLogEntity entity){
	    SysOperationLogRecord record = new SysOperationLogRecord();
	    String id = UUID.randomUUID().toString().replace("-", "");
	    entity.setId(id);
	    record.from(entity);
	    this.dsl.executeInsert(record);
	}
  
	public void deleteById(Integer id) {
		this.dsl.delete(this.OPERATIONLOG).where(new Condition[] { this.OPERATIONLOG.ID.eq(id) }).execute();
	}
}
